package java学习.第18章_JDK8_17新特性;

import java.util.Objects;

/**
 * ClassName: JDK14_16之Record
 * Package: java学习.第18章_JDK8_17新特性
 * Description:
 *
 * @Author 张蛟龙
 * @Create 2023/4/29 21:54
 * @Version 1.0
 */
public class JDK14_16之Record {
    public static void main(String[] args) {
        Order1 o1 = new Order1(1,"张三");
        o1.id();
        o1.name();
        o1.equals(o1);
        Order2 o2 = new Order2(1,"张三");
        o2.id();
        o2.name();
        System.out.println(o1);
        System.out.println(o2);
    }

}
//次等同于 Order2   不能有父类了
record Order1(int id,String name){
    public Order1(){
        this(0,null);
//        super();//public abstract class Record
    }

}
class Order2{
    private final int id;
    private final String name;
    public Order2(int id,String name){
        this.id = id;
        this.name = name;
    }

    public int id() {
        return id;
    }

    public String name() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Order2 order2 = (Order2) o;
        return id == order2.id && Objects.equals(name, order2.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    @Override
    public String toString() {
        return "Order2[" +
                "id=" + id +
                ", name=" + name + ']';
    }
}